﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Public;

namespace DAL
{
    public class KhaiSinhDAL
    {
        public static void InsertKhaiSinh(KhaiSinhPublic _ks)
        {
            KhaiSinh ks = new KhaiSinh();
            ks.MaKS = _ks.MaKS;
            ks.SoKhaiSinh = _ks.SoKhaiSinh;
            ks.NguoiDangKi = _ks.NguoiDangki;
            ks.MaQH = _ks.MaQH;
            ks.NguoiCap = _ks.NguoiCap;
            ks.ChucVu = _ks.ChucVu;
            ks.NgayCap = _ks.NgayCap;
            ks.NoiCap = _ks.NoiCap;
            ks.NoiSinh = _ks.NoiSinh;
            ks.MaNKC = _ks.MaNKC;
            ks.MaNKM = _ks.MaNKM;

            ks.CanBoHoTich = _ks.CanBoHoTich;
            ks.CanBoDangKi = _ks.CanBoDangKi;
            ks.ChucVuNguoiNhan = _ks.ChucVuNguoiNhan;
            ks.NgayNhap = _ks.NgayNhap;
            ks.NguoiNhan = _ks.NguoiNhan;
            Common.db.AddToKhaiSinhs(ks);
            Common.db.SaveChanges();
        }
         public static KhaiSinhPublic LoadKhaiSinhByMaKS(int MaKS)
         {
             var ks = Common.db.ExecuteStoreQuery<KhaiSinhPublic>(@"select MaKS,ISNULL(SoKhaiSinh,0) as SoKhaiSinh,ISNULL(NguoiDangKi,0) as NguoiDangKi,ISNULL(MaQH,0) as MaQH,ISNULL(NguoiCap,0) as NguoiCap,ISNULL(ChucVu,0) as ChucVu,ISNULL(NgayCap,'') as NgayCap,NoiSinh,ISNULL(NoiCap,0) as NoiCap,ISNULL(MaNKC,0) as MaNKC,ISNULL(MaNKM,0) as MaNKM,ISNULL(CanBoDangKi,0) as CanBoDangKi,ISNULL(CanBoHoTich,0) as CanBoHoTich,ISNULL(NgayNhap,'') as NgayNhap,ISNULL(NguoiNhan,0) as NguoiNhan,ISNULL(ChucVuNguoiNhan,0) as ChucVuNguoiNhan from KhaiSinh where MaKS=" + MaKS);
             return ks.FirstOrDefault();
         }
         //public static KhaiSinhPublic UpdateKhaiSinh(int MaKS)
         //{
         //    var ks = Common.db.ExecuteStoreQuery<KhaiSinhPublic>(@"update GiayKhaiSinh set where MaKS=" + MaKS);

         //}
         public static int CountKhaiSinhByYear(int firstyear, int lastyear)
         {
             var ks = Common.db.ExecuteStoreQuery<int>(@"select count(MaKS) from KhaiSinh where year(NgayNhap)>=" + firstyear + "and year(NgayNhap)<=" + lastyear);
             return ks.FirstOrDefault();
         }
    }
}
